//全局变量
var BdMapLevelDefault = 15 ; 					//百度地图默认缩放等级
var BdMapCoordXyDefault = '116.404, 39.915' ; 	//百度地图默认加载坐标

//read COOKIE
function getCookie(cookiename) {
  var result;
  var mycookie = document.cookie;
  var start2 = mycookie.indexOf(cookiename + "=");
  if (start2 > -1) {
    var start = mycookie.indexOf("=", start2) + 1;
    var end = mycookie.indexOf(";", start);

  if (end == -1) {
    end = mycookie.length;
  }

  result = unescape(mycookie.substring(start, end));
  }

  return result;
}
//----------------
//write COOKIE
function setCookie(cookiename, cookievalue, hours) {
  var date = new Date();
  date.setTime(date.getTime() + Number(hours) * 3600 * 1000);
  document.cookie = cookiename + "=" + cookievalue + "; path=/;expires = " + date.toGMTString();

}


(function($){
  $.fn.extend({
  center:function() {
    var e = $(this);
    e.css('top',($(window).height() - e.height())/2 ); 
    e.css('left',($(window).width() - e.width())/2 );
    return e;
  },
  })
})(jQuery);


$(function(){
  $("[idting-role='slidedown']").on('click',function(){
    var control_id = $(this).attr('idting-controlid');
    var control = $("#" + control_id);
    if(control.css('display') != 'none'){
        control.slideUp()
        $(this).removeClass('icon_ad').addClass('icon_ar')
    }else{
        control.slideDown()
        $(this).removeClass('icon_ar').addClass('icon_ad')
    }
  })


  //$("a:contains(删除)").remove()
	$("input[type='button']:contains('新增酒店')").css('background','white');
})

/**
格式化日期格式 供日历用
2015-3-1 -> 201531
*/
function formatCalendarDate(dateStr){
	return dateStr.replace(/-/g, "");
}

/**
js in_array 
*/
Array.prototype.in_array = function(e){
	for(i=0;i<this.length && this[i]!=e;i++);
	return !(i==this.length);
}

/* 去掉最后一行border,后台 */
$(function(){
  $(".ven_ul li:last").css("border","none");
 
})



function isTimeBeforeNow(d){
	return getDate(d) < new Date();
}

/**
  地图坐标格式化
*/
function mapCoord2XY(mapCoord){
	console.log(mapCoord);
	if(!mapCoord || mapCoord == '' || mapCoord == 'undefined'){
		mapCoord = BdMapCoordXyDefault ;
	}
	var xyArrTemp = mapCoord.split(',') ;
	var xyArr = [] ;
	for(i in xyArrTemp){
		xyArr.push(parseFloat(xyArrTemp[i])) ;
	}
	return xyArr ;
}

/**
  地图缩放级别格式化
*/
function mapLevelFormat(mapLevel){
  if(mapLevel == 'undefined'){return mapLevelDefault ;}
  mapLevel = parseInt(mapLevel) ;
  if(mapLevel == 0){return BdMapLevelDefault ;}
  return mapLevel ;
}

/**
	百度地图初始化
	调取方法：
	引用 <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=<?php echo BAIDU_MAP_AK ; ?>" ></script>
	放置地图容器 <div class="hoteldetail_info_left_map" id='allmap' mapCoord='坐标' mapLevel='缩放等级' ></div>
	页面加载完成调用  initBaiduMap('容器ID') ;
*/
function initBaiduMap(id){
	if(!id){return ''}
	var _mapContainer = $('#'+id) ;
	// 百度地图API功能
	var mapCoord =  _mapContainer.attr('mapCoord') ;
	var mapLevel = _mapContainer.attr('mapLevel') ;
	var xyCoord = mapCoord2XY(mapCoord);
	var map = new BMap.Map(id);
	var point = new BMap.Point(xyCoord[0],xyCoord[1]);
	map.centerAndZoom(point, 15);
	var marker = new BMap.Marker(point);  			// 创建标注
	map.addOverlay(marker);               			// 将标注添加到地图中
	marker.setAnimation(BMAP_ANIMATION_BOUNCE); 	//跳动的动画
}

//弹出框
function showMsg(str,time){
  if($("#alertMsg").attr('id') == undefined){
    var msg = "<div id='alertMsg'><div class='alertmsg_close'>x</div>"+str+"</div>";
    $("body").append(msg);
    msgObj = $("#alertMsg");
    msgObj.css('top',($(window).height()-msgObj.height())/2-100);
    msgObj.css('left',($(document).width()-msgObj.width())/2 - 15);
    if( !time ) {
        time = 60000000000;
    }
    $("#alertMsg").show().fadeOut(time, function() {
      $("#alertMsg").remove();
    });
  }
}
$('.alertmsg_close').live('click',function(){
  $("#alertMsg").remove();
});

//初始化loading控件
function initLoadingBar(){ 
  var canvasBox = '<canvas style="display:none;position:fixed;z-index:10000;" id="canvas"></canvas>';
  $('body').prepend(canvasBox);
  var canvas = document.getElementById('canvas');
  $(canvas).css('margin-left',($(window).width()-$(canvas).width())/2);
  $(canvas).css('margin-top',($(window).height()-$(canvas).height())/2);
  //$(canvas).css({'width':$(window).width(),'height':$(window).height()});
  return new loading(canvas,{radius:8,circleLineWidth:3});
}

//加载中插件
function loading(canvas,options){   
    this.canvas = canvas;   
    if(options){   
      this.radius = options.radius||12;   
      this.circleLineWidth = options.circleLineWidth||4;   
      this.circleColor = options.circleColor||'lightgray';   
      this.dotColor = options.dotColor||'gray';   
    }else{         
      this.radius = 12;   
      this.circelLineWidth = 4;   
      this.circleColor = 'lightgray';   
      this.dotColor = 'gray';   
    }   
}   
loading.prototype = {   
  show:function (){   
    var canvas = this.canvas;   
    if(!canvas.getContext)return;   
    if(canvas.__loading)return;   
    canvas.__loading = this;   
    var ctx = canvas.getContext('2d');   
    var radius = this.radius;         
    var rotators = [{angle:0,radius:1.5},{angle:3/radius,radius:2},{angle:7/radius,radius:2.5},{angle:12/radius,radius:3}];         
    var me = this;   
    canvas.loadingInterval = setInterval(function(){   
      ctx.clearRect(0,0,canvas.width,canvas.height);            
      var lineWidth = me.circleLineWidth;   
      var center = {x:canvas.width/2 - radius,y:canvas.height/2-radius};             
      ctx.beginPath();   
      ctx.lineWidth = lineWidth;   
      ctx.strokeStyle = me.circleColor;   
      ctx.arc(center.x,center.y,radius,0,Math.PI*2);   
      ctx.closePath();   
      ctx.stroke();   
      for(var i=0;i<rotators.length;i++){           
        var rotatorAngle = rotators[i].currentAngle||rotators[i].angle;               
        //在圆圈上面画小圆   
        var rotatorCenter = {x:center.x-(radius)*Math.cos(rotatorAngle) ,y:center.y-(radius)*Math.sin(rotatorAngle)};               
        var rotatorRadius = rotators[i].radius;   
        ctx.beginPath();   
        ctx.fillStyle = me.dotColor;   
        ctx.arc(rotatorCenter.x,rotatorCenter.y,rotatorRadius,0,Math.PI*2);   
        ctx.closePath();   
        ctx.fill();   
        rotators[i].currentAngle = rotatorAngle+4/radius;   
      }   
    },50);   
    $("button[type='submit']").attr('disabled',true);
    canvas.style.display = "block";
  },   
  hide:function(){   
    var canvas = this.canvas;   
    canvas.__loading = false;   
    if(canvas.loadingInterval){   
      window.clearInterval(canvas.loadingInterval);   
    }   
    var ctx = canvas.getContext('2d');   
    if(ctx)ctx.clearRect(0,0,canvas.width,canvas.height);  
    $("button[type='submit']").attr('disabled',false); 
    canvas.style.display = "none";
  }   
};   


function getParam(name,default_value) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return default_value;
}

function getParams() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

function submit_order(product_model,id){
  var url = '/pay/order.php?model=' + product_model + "&id="+id;
  var number = $('#use_number').val();
  var date = $('#datepicker').val();
  if(date.length < 1){
    showMsg('请选择出游日期',4000) ;
    return '' ;
  }
  var price = $('#_calendar_prices_nprice').text();
  url += '&number='+ number;
  url += '&date=' + date;
  url += '&price=' + price;

  window.location.href = url;
}


//字符串转日期格式，strDate要转为日期格式的字符串
function getDate(strDate) {
    var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
     function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
    return date;
}


$(document).ready(function(){ 
  /* select 样式修改/全部 */
  $(".sel_wrap").on("change", function() {
    var o;
    var opt = $(this).find('option');
    opt.each(function(i) {
      if (opt[i].selected == true) {
        o = opt[i].innerHTML;
      }
    })
    $(this).find('label').html(o);
  }).trigger('change');

  // 详情页 去掉边距 你可能还感兴趣的 
  $("ul.detial_bottom_list").each(function(){
    $(this).find('li:last').css({"margin-right":"0"})
  });

  //详情页订单人数修改
  var use_number = $('#use_number') ;
  use_number.parent().prev().find('a').click(function(){
    var use_number_val = parseInt(use_number.val()) ;
    use_number.val(Math.max(1,use_number_val-1)) ;
  });
  use_number.parent().next().find('a').click(function(){
    var use_number_val = parseInt(use_number.val()) ;
    use_number.val(use_number_val+1) ;
  });

}); 


Array.prototype.indexOf = function(val) {              
    for (var i = 0; i < this.length; i++) {  
        if (this[i] == val) return i;  
    }  
    return -1;  
}

Array.prototype.remove = function(val) {  
    var index = this.indexOf(val);  
    if (index > -1) {  
        this.splice(index, 1);  
    }  
}

function move(id){
  $('#'+id).remove()
}

function closeBox(id){
  $('#'+id).hide()
}

function jsonPrama2Url(o){
  var url_condition = '' ;
  var connector = '?' ;
  for(i in o){
    if(!o[i]){continue;}
    connector = url_condition.length < 1 ? '?' : '&' ;
    url_condition += connector+i+'='+o[i] ;
  }
  return url_condition ;
}